package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.ActCategory;
import com.ruoyi.system.domain.Game;
import com.ruoyi.system.mapper.ActCategoryMapper;
import com.ruoyi.system.mapper.GameMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ActTitleMapper;
import com.ruoyi.system.domain.ActTitle;
import com.ruoyi.system.service.IActTitleService;
import com.ruoyi.common.core.text.Convert;

/**
 * 命题管理Service业务层处理
 *
 * @author xuyk
 * @date 2021-11-29
 */
@Service("actTitleService")
public class ActTitleServiceImpl implements IActTitleService
{
    @Autowired
    private ActTitleMapper actTitleMapper;

    @Autowired
    private GameMapper gameMapper;

    @Autowired
    private ActCategoryMapper actCategoryMapper;

    /**
     * 查询命题管理
     *
     * @param id 命题管理主键
     * @return 命题管理
     */
    @Override
    public ActTitle selectActTitleById(Long id)
    {
        return actTitleMapper.selectActTitleById(id);
    }

    /**
     * 查询命题管理列表
     *
     * @param actTitle 命题管理
     * @return 命题管理
     */
    @Override
    public List<ActTitle> selectActTitleList(ActTitle actTitle)
    {
        return actTitleMapper.selectActTitleList(actTitle);
    }

    /**
     * 通过大会ID和类别ID 查询命题管理列表
     * @param gameId
     * @param categoryId
     * @return
     */
    @Override
    public List<ActTitle> selectByGameIdAndCategoryId(Long gameId,Long categoryId){
        ActTitle actTitle = new ActTitle();
        actTitle.setGameId(gameId);
        actTitle.setCategoryId(categoryId);
        return actTitleMapper.selectActTitleList(actTitle);
    }

    /**
     * 通过大会ID 查询命题
     * @param gameId
     * @return
     */
    @Override
    public List<ActTitle> selectByGameId(Long gameId){
        return actTitleMapper.selectActTitleByGameId(gameId);
    }


    /**
     * 新增命题管理
     *
     * @param actTitle 命题管理
     * @return 结果
     */
    @Override
    public int insertActTitle(ActTitle actTitle)
    {
        //获取大赛名称
        if(actTitle.getGameId() != null){
            Game game = gameMapper.selectGameById(actTitle.getGameId());
            if(game != null){
                actTitle.setGameName(game.getName());
            }
        }
//        if(actTitle.getCategoryId() != null){
//            ActCategory actCategory = actCategoryMapper.selectActCategoryById(actTitle.getCategoryId());
//            if(actCategory != null){
//                actTitle.setCategoryName(actCategory.getName());
//            }
//        }
        actTitle.setCreateTime(DateUtils.getNowDate());
        return actTitleMapper.insertActTitle(actTitle);
    }

    /**
     * 修改命题管理
     *
     * @param actTitle 命题管理
     * @return 结果
     */
    @Override
    public int updateActTitle(ActTitle actTitle)
    {
        //获取大赛名称
        if(actTitle.getGameId() != null){
            Game game = gameMapper.selectGameById(actTitle.getGameId());
            if(game != null){
                actTitle.setGameName(game.getName());
            }
        }
//        if(actTitle.getCategoryId() != null){
//            ActCategory actCategory = actCategoryMapper.selectActCategoryById(actTitle.getCategoryId());
//            if(actCategory != null){
//                actTitle.setCategoryName(actCategory.getName());
//            }
//        }
        actTitle.setUpdateTime(DateUtils.getNowDate());
        return actTitleMapper.updateActTitle(actTitle);
    }

    /**
     * 批量删除命题管理
     *
     * @param ids 需要删除的命题管理主键
     * @return 结果
     */
    @Override
    public int deleteActTitleByIds(String ids)
    {
        return actTitleMapper.deleteActTitleByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除命题管理信息
     *
     * @param id 命题管理主键
     * @return 结果
     */
    @Override
    public int deleteActTitleById(Long id)
    {
        return actTitleMapper.deleteActTitleById(id);
    }
}
